#include "player.h"
#include <QDebug>

Player::Player()
{


}

Player::~Player(){
    //destroy things
}

QString Player::getFirstName(){
    return firstName;
}

void Player::setFirstName(QString inputFirstName){
    firstName=inputFirstName;
}

QString Player::getMiddleName(){
    return middleName;
}

void Player::setMiddleName(QString inputMiddleName){
    middleName=inputMiddleName;
}

QString Player::getLastName(){
    return lastName;
}

void Player::setLastName(QString inputLastName){
    lastName=inputLastName;
}

void Player::setId(int i) {
        player_id = i;
    }

int Player::getId(){
        return player_id;
    }

PlayerStatistics Player::getPlayerStats(){
    return playerStats;
}

void Player::setJerseyNumber(int number){
    jerseyNumber=number;
}

int Player::getJerseyNumber(){
    return jerseyNumber;
}

void Player::addPlayerToDB() {

    QSqlDatabase appDb = QSqlDatabase::addDatabase("QSQLITE");
    appDb.setDatabaseName("/sdcard/basedata/base.db.sqlite");
    appDb.open();
    // qDebug() << appDb.lastError().text();

    QSqlQuery query("CREATE TABLE IF NOT EXISTS players(first varchar(30), middle varchar(30), last varchar(30), jersy int, u_id  INTEGER PRIMARY KEY AUTOINCREMENT)");
    QString insert_query = QString("insert into players values('%1','%2','%3', %4, NULL)").arg(firstName).arg(middleName).arg(lastName).arg(jerseyNumber);


    query.prepare(insert_query);
    query.exec();

            // Check the ID of this player - its being given next available by the database.

            QString check_id = QString("SELECT MAX(u_id) FROM players");
            query.prepare(check_id);
            query.exec();
            int next_free_id = 0;

              while(query.next()) {
                // qDebug() << "Id:" << query.value(0).toString();
                next_free_id = query.value(0).toInt();
              }







            // Create Table for player stats.  Each row is 1 game. player_id and game_id are used to differenciate between games.

        QString create_stats_table = QString("CREATE TABLE IF NOT EXISTS player_stats(atBats int, singles int, doubles int, triples int, grandSlams int, homeruns int, walks int, strikeouts int, runsScored int, runsBattedIn int, stolenBases int, sacrificeFlys int, walksPitched int, earnedRuns int, inningsPitched int, strikeoutsPitched int, wins int, losses int, hitsAllowed int, atBatsFaced int, errors int, game_id int, player_id int )");

            query.prepare(create_stats_table);
            query.exec();

            // Push History to database.
            qDebug() << getPlayerStats().getPriorHistory().getAtBats();

            QString instert_history_stats = QString("INSERT INTO player_stats VALUES(%1 , %2, %3, %4, %5, %6 , %7 , %8 , %9 , %10 , %11 , %12 , %13 , %14 ,  %15 ,  %16 , %17 ,  %18 ,  %19 ,  %20 ,  %21 ,  %22 ,  %23)")
                    .arg(this->getPlayerStats().getPriorHistory().getAtBats())
                    .arg(this->getPlayerStats().getPriorHistory().getSingles())
                    .arg(this->getPlayerStats().getPriorHistory().getDoubles())
                    .arg(this->getPlayerStats().getPriorHistory().getTriples())
                    .arg(this->getPlayerStats().getPriorHistory().getGrandSlams())
                    .arg(this->getPlayerStats().getPriorHistory().getHomeRuns())
                    .arg(this->getPlayerStats().getPriorHistory().getWalks())
                    .arg(this->getPlayerStats().getPriorHistory().getStrikeouts())
                    .arg(this->getPlayerStats().getPriorHistory().getRunsScored())
                    .arg(this->getPlayerStats().getPriorHistory().getRunsBattedIn())
                    .arg(this->getPlayerStats().getPriorHistory().getStolenBases())
                    .arg("0")
                    .arg(this->getPlayerStats().getPriorHistory().getWalksPitched())
                    .arg(this->getPlayerStats().getPriorHistory().getEarnedRuns())
                    .arg(this->getPlayerStats().getPriorHistory().getInningsPitched())
                    .arg(this->getPlayerStats().getPriorHistory().getStrikeoutsPitched())
                    .arg(this->getPlayerStats().getPriorHistory().getWins())
                    .arg(this->getPlayerStats().getPriorHistory().getLosses())
                    .arg(this->getPlayerStats().getPriorHistory().getHitsAllowed())
                    .arg(this->getPlayerStats().getPriorHistory().getAtBatsFaced())
                    .arg(this->getPlayerStats().getPriorHistory().getErrors())
                    .arg("0") // 0 represents History stats
                    .arg(next_free_id);

            qDebug() << instert_history_stats;

            query.prepare(instert_history_stats);
            query.exec();


}
